package com.huluobo.a2109amvvmdemo.lifecycle;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.OnLifecycleEvent;

import com.huluobo.a2109amvvmdemo.R;

/**
 * Created by LC on 2023/11/28.
 */
public class TipDialog extends Dialog implements LifecycleObserver {
    public TipDialog(@NonNull Context context) {
        super(context);
        if (context instanceof AppCompatActivity) {
            ((AppCompatActivity) context).getLifecycle().addObserver(this);//直接从activity中拿到宿主对象,来添加订阅关系
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    public void start() {
        show();
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    public void stop() {
        dismiss();
    }
}
